package hjslm.Controller.Front;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import hjslm.Bean.NavcustomBean;
import hjslm.Bean.ShijuanBean;
import hjslm.Service.IBaseTwoService;
import hjslm.Service.INavCustomService;
import hjslm.Service.IShijuanService;
import hjslm.Service.IWeixinService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import hjslm.Common.Impl.FileOperateUtilImpl;
import hjslm.Bean.BaseTwoBean;
import framework.base.common.Pager;
import framework.base.controller.BaseController;


@Controller
@RequestMapping("/paper")
public class FShijuanController extends BaseController{

	/**
	 * 试卷控制层
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	IBaseTwoService basetwoService;

	@Autowired
	IShijuanService shijuanService;
	
	@Autowired
	INavCustomService navCustomService;
	
	@Autowired
	IWeixinService weixinService;
	
	@Override
	public void setLogger() {
	}
	
	@RequestMapping("/index")
	public ModelAndView index(ShijuanBean bean, Pager<ShijuanBean> pager){
		ModelAndView modelAndView = new ModelAndView("front/shijuan/index");
		modelAndView.addObject("bean", bean);
		pager = shijuanService.selectPage(bean, pager);
		modelAndView.addObject("pager", pager);
		
		//基本信息
		List<BaseTwoBean> listbt = basetwoService.selectList(null);
		List<BaseTwoBean> listGrade = new ArrayList<BaseTwoBean>();
		List<BaseTwoBean> listSubject = new ArrayList<BaseTwoBean>();
		List<BaseTwoBean> listYear = new ArrayList<BaseTwoBean>();
		List<BaseTwoBean> liststype = new ArrayList<BaseTwoBean>();
		for (BaseTwoBean baseTwoBean : listbt) {
			switch (baseTwoBean.getBotype()) {
			case "试卷年级":
				listGrade.add(baseTwoBean);
				break;
			case "试卷科目":
				listSubject.add(baseTwoBean);
				break;
			case "试卷年份":
				listYear.add(baseTwoBean);
				break;
			case "试卷类型":
				liststype.add(baseTwoBean);
				break;
			}
		}
		modelAndView.addObject("listGrade", listGrade);
		modelAndView.addObject("listSubject", listSubject);
		modelAndView.addObject("listYear", listYear);
		modelAndView.addObject("liststype", liststype);
		
		List<NavcustomBean> navList  = navCustomService.selectList(null);
		modelAndView.addObject("navList", navList);
		modelAndView.addObject("map", weixinService.selectMap());
		return modelAndView;
	}
	
	@RequestMapping("/info")
	public ModelAndView info(ShijuanBean bean){
		ModelAndView modelAndView = new ModelAndView("front/shijuan/info");
		bean = shijuanService.selectOne(bean);
		modelAndView.addObject("bean", bean);
		
		List<NavcustomBean> navList  = navCustomService.selectList(null);
		modelAndView.addObject("navList", navList);
		modelAndView.addObject("map", weixinService.selectMap());
		return modelAndView;
	}
	
	@RequestMapping("/download")
	public void download(String storeName,String realName,HttpSession session,HttpServletRequest request,HttpServletResponse response) throws Exception  {
		String contentType = "application/octet-stream";
		String tmp = storeName.substring(storeName.lastIndexOf("."));
		realName += tmp;
		FileOperateUtilImpl.download(request, response, storeName, contentType,realName);
	}

}
